package com.tiecode.plugin.pvz.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.net.http.SslError;
import android.view.View;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import androidx.annotation.NonNull;

import com.tiecode.develop.component.api.multiple.Window;

import java.net.URI;

public class PVZView extends WebView implements Window {
    private URI uri;

    public PVZView(@NonNull Context context) {
        super(context);
        initView();
    }

    @SuppressLint("SetJavaScriptEnabled")
    private void initView() {
        WebSettings settings = getSettings();
        settings.setSupportMultipleWindows(true);
        settings.setDomStorageEnabled(true);
        settings.setDisplayZoomControls(false);
        settings.setSupportZoom(true);
        settings.setBuiltInZoomControls(true);
        settings.setUseWideViewPort(true);
        settings.setJavaScriptEnabled(true);
        this.setScrollBarSize(0);

        this.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }

            @SuppressLint("WebViewClientOnReceivedSslError")
            @Override
            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                handler.proceed();
            }
        });
    }

    @Override
    public View getView() {
        return this;
    }

    @Override
    public String getName() {
        return "植物大战僵尸";
    }

    @Override
    public URI getOpenedURI() {
        return uri;
    }

    @Override
    public void openURI(URI uri) {
        this.uri = uri;
        this.loadUrl("http://pvz.56wan.com");
    }

    @Override
    public void updateURI(URI uri) {
        this.uri = uri;
    }

    @Override
    public void onClose() {
        this.uri = null;
    }
}
